//
//  CYGeRenZhuYeWenDaTableViewCell.m
//  cyydt
//
//  Created by chu on 2019/2/14.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYGeRenZhuYeWenDaTableViewCell.h"

@implementation CYGeRenZhuYeWenDaTableViewCell

+ (instancetype)initWithTableView:(UITableView *)tableView{
    CYGeRenZhuYeWenDaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYGeRenZhuYeWenDaTableViewCell"];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CYGeRenZhuYeWenDaTableViewCell" owner:self options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.headImageView.layer.cornerRadius = self.headImageView.frame.size.height / 2;
    self.headImageView.layer.masksToBounds = YES;
}

- (void)setModel:(CYProjectModel *)model{
    _model = model;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:model.answer_avatar] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headImageView.frame.size]];
    self.nicknameLabel.text = model.answer_nickname;
    self.typeLabel.text = @"回答了";
    self.timeLabel.text = model.answer_time;
    
    CGFloat contentHeight = [[Tool sharedInstance] heightForString:model.answer_content andWidth:kWindowW - 30 fontSize:16];
    NSArray *lines = [[Tool sharedInstance] getLinesArrayOfStringWithString:model.answer_content Font:[UIFont systemFontOfSize:16] Rect:CGRectMake(0, 0, kWindowW - 30, MAXFLOAT)];
    if (lines.count >= 2) {
        self.titleViewHeightConstraint.constant = 45;
    }else{
        self.titleViewHeightConstraint.constant = contentHeight;
    }
    self.titleLabel.text = model.answer_content;

    
    [self addSeeMoreButton];
}

- (void)addSeeMoreButton {
    __weak typeof(self) _self = self;
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"...全文"];
    
    YYTextHighlight *hi = [YYTextHighlight new];
    [hi setColor:[UIColor colorWithRed:0.578 green:0.790 blue:1.000 alpha:1.000]];
    hi.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
        
    };
    
    [text yy_setColor:[UIColor colorWithRed:0.000 green:0.449 blue:1.000 alpha:1.000] range:[text.string rangeOfString:@"全文"]];
    [text yy_setTextHighlight:hi range:[text.string rangeOfString:@"全文"]];
    text.yy_font = self.titleLabel.font;
    
    YYLabel *seeMore = [YYLabel new];
    seeMore.attributedText = text;
    [seeMore sizeToFit];
    
    NSAttributedString *truncationToken = [NSAttributedString yy_attachmentStringWithContent:seeMore contentMode:UIViewContentModeCenter attachmentSize:seeMore.size alignToFont:text.yy_font alignment:YYTextVerticalAlignmentCenter];
    self.contentLabel.truncationToken = truncationToken;
}

@end
